1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  import com.google.common.base.Objects;
21  
22  import java.util.Collection;
23  import java.util.Iterator;
24  
25  import javax.annotation.Nullable;
26  
27  /**
28   * A collection which forwards all its method calls to another collection.
29   * Subclasses should override one or more methods to modify the behavior of the
30   * backing collection as desired per the <a
31   * href="http://en.wikipedia.org/wiki/Decorator_pattern">decorator pattern</a>.
32   *
33   * <p><b>Warning:</b> The methods of {@code ForwardingCollection} forward
34   * <b>indiscriminately</b> to the methods of the delegate. For example,
35   * overriding {@link #add} alone <b>will not</b> change the behavior of {@link
36   * #addAll}, which can lead to unexpected behavior. In this case, you should
37   * override {@code addAll} as well, either providing your own implementation, or
38   * delegating to the provided {@code standardAddAll} method.
39   *
40   * <p>The {@code standard} methods are not guaranteed to be thread-safe, even
41   * when all of the methods that they depend on are thread-safe.
42   *
43   * @author Kevin Bourrillion
44   * @author Louis Wasserman
45   * @since 2.0 (imported from Google Collections Library)
46   */
47  @GwtCompatible
48  public abstract class ForwardingCollection<E> extends ForwardingObject
49      implements Collection<E> {
50    // TODO(user): identify places where thread safety is actually lost
51  
52    /** Constructor for use by subclasses. */
53    protected ForwardingCollection() {}
54  
55    @Override protected abstract Collection<E> delegate();
56  
57    @Override
58    public Iterator<E> iterator() {
59      return delegate().iterator();
60    }
61  
62    @Override
63    public int size() {
64      return delegate().size();
65    }
66  
67    @Override
68    public boolean removeAll(Collection<?> collection) {
69      return delegate().removeAll(collection);
70    }
71  
72    @Override
73    public boolean isEmpty() {
74      return delegate().isEmpty();
75    }
76  
77    @Override
78    public boolean contains(Object object) {
79      return delegate().contains(object);
80    }
81  
82    @Override
83    public boolean add(E element) {
84      return delegate().add(element);
85    }
86  
87    @Override
88    public boolean remove(Object object) {
89      return delegate().remove(object);
90    }
91  
92    @Override
93    public boolean containsAll(Collection<?> collection) {
94      return delegate().containsAll(collection);
95    }
96  
97    @Override
98    public boolean addAll(Collection<? extends E> collection) {
99      return delegate().addAll(collection);
100   }
101 
102   @Override
103   public boolean retainAll(Collection<?> collection) {
104     return delegate().retainAll(collection);
105   }
106 
107   @Override
108   public void clear() {
109     delegate().clear();
110   }
111 
112   @Override
113   public Object[] toArray() {
114     return delegate().toArray();
115   }
116 
117   @Override
118   public <T> T[] toArray(T[] array) {
119     return delegate().toArray(array);
120   }
121 
122   /**
123    * A sensible definition of {@link #contains} in terms of {@link #iterator}.
124    * If you override {@link #iterator}, you may wish to override {@link
125    * #contains} to forward to this implementation.
126    *
127    * @since 7.0
128    */
129   protected boolean standardContains(@Nullable Object object) {
130     return Iterators.contains(iterator(), object);
131   }
132 
133   /**
134    * A sensible definition of {@link #containsAll} in terms of {@link #contains}
135    * . If you override {@link #contains}, you may wish to override {@link
136    * #containsAll} to forward to this implementation.
137    *
138    * @since 7.0
139    */
140   protected boolean standardContainsAll(Collection<?> collection) {
141     return Collections2.containsAllImpl(this, collection);
142   }
143 
144   /**
145    * A sensible definition of {@link #addAll} in terms of {@link #add}. If you
146    * override {@link #add}, you may wish to override {@link #addAll} to forward
147    * to this implementation.
148    *
149    * @since 7.0
150    */
151   protected boolean standardAddAll(Collection<? extends E> collection) {
152     return Iterators.addAll(this, collection.iterator());
153   }
154 
155   /**
156    * A sensible definition of {@link #remove} in terms of {@link #iterator},
157    * using the iterator's {@code remove} method. If you override {@link
158    * #iterator}, you may wish to override {@link #remove} to forward to this
159    * implementation.
160    *
161    * @since 7.0
162    */
163   protected boolean standardRemove(@Nullable Object object) {
164     Iterator<E> iterator = iterator();
165     while (iterator.hasNext()) {
166       if (Objects.equal(iterator.next(), object)) {
167         iterator.remove();
168         return true;
169       }
170     }
171     return false;
172   }
173 
174   /**
175    * A sensible definition of {@link #removeAll} in terms of {@link #iterator},
176    * using the iterator's {@code remove} method. If you override {@link
177    * #iterator}, you may wish to override {@link #removeAll} to forward to this
178    * implementation.
179    *
180    * @since 7.0
181    */
182   protected boolean standardRemoveAll(Collection<?> collection) {
183     return Iterators.removeAll(iterator(), collection);
184   }
185 
186   /**
187    * A sensible definition of {@link #retainAll} in terms of {@link #iterator},
188    * using the iterator's {@code remove} method. If you override {@link
189    * #iterator}, you may wish to override {@link #retainAll} to forward to this
190    * implementation.
191    *
192    * @since 7.0
193    */
194   protected boolean standardRetainAll(Collection<?> collection) {
195     return Iterators.retainAll(iterator(), collection);
196   }
197 
198   /**
199    * A sensible definition of {@link #clear} in terms of {@link #iterator},
200    * using the iterator's {@code remove} method. If you override {@link
201    * #iterator}, you may wish to override {@link #clear} to forward to this
202    * implementation.
203    *
204    * @since 7.0
205    */
206   protected void standardClear() {
207     Iterators.clear(iterator());
208   }
209 
210   /**
211    * A sensible definition of {@link #isEmpty} as {@code !iterator().hasNext}.
212    * If you override {@link #isEmpty}, you may wish to override {@link #isEmpty}
213    * to forward to this implementation. Alternately, it may be more efficient to
214    * implement {@code isEmpty} as {@code size() == 0}.
215    *
216    * @since 7.0
217    */
218   protected boolean standardIsEmpty() {
219     return !iterator().hasNext();
220   }
221 
222   /**
223    * A sensible definition of {@link #toString} in terms of {@link #iterator}.
224    * If you override {@link #iterator}, you may wish to override {@link
225    * #toString} to forward to this implementation.
226    *
227    * @since 7.0
228    */
229   protected String standardToString() {
230     return Collections2.toStringImpl(this);
231   }
232 
233   /**
234    * A sensible definition of {@link #toArray()} in terms of {@link
235    * #toArray(Object[])}. If you override {@link #toArray(Object[])}, you may
236    * wish to override {@link #toArray} to forward to this implementation.
237    *
238    * @since 7.0
239    */
240   protected Object[] standardToArray() {
241     Object[] newArray = new Object[size()];
242     return toArray(newArray);
243   }
244 
245   /**
246    * A sensible definition of {@link #toArray(Object[])} in terms of {@link
247    * #size} and {@link #iterator}. If you override either of these methods, you
248    * may wish to override {@link #toArray} to forward to this implementation.
249    *
250    * @since 7.0
251    */
252   protected <T> T[] standardToArray(T[] array) {
253     return ObjectArrays.toArrayImpl(this, array);
254   }
255 }